<template>
  <page-container title="站点管理">
    <template #extra>
      <el-button @click="onAddPoint">添加站点</el-button>
    </template>
    <div class="mt-4">
      <el-input
        v-model="query"
        style="max-width: 600px"
        placeholder="请输入查询信息"
        class="input-with-select"
        clearable
        @clear="clear"
      >
        <template #append>
          <el-button :icon="Search" @click="findByQuery" />
        </template>
      </el-input>
    </div>
    <!-- v-loading="loading"这个是页面加载的loading效果 -->
    <el-table v-loading="loading" style="width: 100%; margin-top: 15px" :data="PointList">
      <el-table-column label="序号" width="250" type="index"></el-table-column>
      <el-table-column label="站点名称" prop="pointName"></el-table-column>
      <el-table-column label="维度" prop="latitude"></el-table-column>
      <el-table-column label="经度" prop="longitude"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEdit(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="onDelPoint(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 添加分类弹框 -->
    <PointEdit ref="dialog" @success="onSuccess"></PointEdit>
  </page-container>
</template>

<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { DelPointService, GetPointService, GetPointFindByQueryService } from '@/api/point'
import PointEdit from './components/PointEdit.vue'
import { Search } from '@element-plus/icons-vue'

const dialog = ref()

const query = ref('')
const findByQuery = async () => {
  loading.value = true
  console.log(query.value)
  const res = await GetPointFindByQueryService(query.value)
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  PointList.value = res.data.data
  loading.value = false
}

const clear = () => {
  query.value = ''
  getPointList()
}

const PointList = ref([])
// loading加载页面效果
const loading = ref(false)

const getPointList = async () => {
  loading.value = true
  const res = await GetPointService()
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  PointList.value = res.data.data
  loading.value = false
}

getPointList()

//点击添加分类跳出弹框
const onAddPoint = () => {
  // dialogFormVisible.value = true
  dialog.value.open({})
}
//点击编辑的时候跳出弹框
const handleEdit = (row) => {
  dialog.value.open(row)
}

//点击删除,删除所在列表
const onDelPoint = async (row) => {
  await ElMessageBox.confirm('你确认要删除该站点么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await DelPointService(row.id)
  ElMessage.success('删除成功')
  getPointList()
}

const onSuccess = () => {
  getPointList()
}
</script>
